<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 所有控制器的基类
 *
 */
class HYD_Controller extends CI_Controller {

    /**
     * organization_id
     *
     * @access protected
     * @var string
     */
    private $_organization_id = NULL;

    /**
     * organization_max
     *
     * @access private
     * @var string
     */
    private $_organization_max = NULL;

    /**
     * organization_min
     *
     * @access private
     * @var string
     */
    private $_organization_min = NULL;

    /**
     * group_id
     *
     * @access protected
     * @var string
     */
    private $_group_id = NULL;

    /**
     * user_name
     *
     * @access protected
     * @var string
     */
    private $_user_name = NULL;
    protected $_module_name = NULL;
    protected $_helpid = NULL;
    /**
     * $field lists
     *
     * @access protected
     * @var array
     */
    private $_field_lists = NULL;

    /** 用作控制器与前台对应视图语言进行匹配
     * 对应help/item/fd_view字段
     * @var type 
     */

    /**
     * Constructor
     *
     * @access public
     * @param string
     */
    public function __construct() {
        parent::__construct();
        $this->_field_lists = array();

//load cache
        $this->load->driver('cache');
        

        //$this->_organization_id = $this->admin->get_organization_id();
        //$this->group_id = $this->admin->get_group_id();
        //$this->_user_name = $this->admin->get_user_name();

        if ($this->input->get_post('helpid') != '') {
            $this->_helpid = $this->input->get_post('helpid');
        } else {
            $this->_helpid = $this->input->get_post('module');
        }
        $this->_module_name = $this->input->get_post('module');
    }

// --------------------------------------------------------------------
    /**
     * 设置全部字段信息
     * @param type $field_array 字段设置
     */
    public function set_field_lists($field_array) {
        $this->_field_lists = $field_array;
    }

    /**
     * 获取全或指定字段信息
     * @param type $field_name 字段名
     * @return type
     */
    public function get_field_lists($field_name = NULL) {
        if (is_null($field_name)) {
            return $this->_field_lists;
        } else {
            return $this->_field_lists[$field_name];
        }
    }

    /**
     * 获取指定字段属性
     * @param type $field_name 字段名
     * @param type $field_item 属性名
     * @return type
     */
    public function get_field_item($field_name, $field_item) {

        return $this->_field_lists[$field_name][$field_item];
    }

    /**
     * 获取下拉、多选、单选列表数据
     * @param type $field_name
     * @return type
     */
    public function get_dropdown_data($field_name) {
        if (!in_array($this->_field_lists['ftype'], array('dropdown', 'dropdownedit', 'dropdowntree', 'checkbox', 'radio'))) {
            return array();
        }
        $datasource = $this->_field_lists[$field_name]['datasource'];
        if (is_array($datasource)) {
            return $datasource;
        } else if (is_callable(array(get_class($this), $datasource))) {
            $numargs = func_num_args();
            if ($numargs > 1) {
                $arg_list = func_get_args();
                unset($arg_list[0]);
                return $this->$datasource($arg_list);
            } else {
                return $this->$datasource();
            }
        }
    }

    /**
     * set_validation_rules
     * @parm array $validation_config 验证规则数组
     * @access protected
     * @return void
     * 
     * $validation_config=array( array('field'=>'fd_user_name', 'label'=>'lang:feild_user_name', 'rules'=>'required') ,...);
     * 模块的语言文件提供first_name的翻译: 
     * zh_CN   feild_user_name = 用户名
     * en_US   feild_user_name = Username
     */
    protected function set_validation_rules($validation_config) {
        $this->form_validation->set_rules($validation_config);
    }

    /**
     * set_validation_data
     * @parm array $data 验证数据(一维数组)
     * @access protected
     * @return void  
     */
    protected function set_validation_data($data) {
        $this->form_validation->set_value($data);
    }

    /**
     * 验证表单
     * @access protected
     * @return bool  
     */
    protected function do_validation() {
        return $this->form_validation->run();
    }

    /**
     * 获取验证提示信息
     * @access protected
     * @return string  
     */
    protected function get_validation_errors() {
        return validation_errors();
    }

    /**
     * 取搜索中的比较操作符、逻辑运算符的下拉选单
     * @param type $key
     * @return type
     */
    protected function get_search_operator_list() {
        $key = $this->input->get_post('key');
        $operator_list = array();
        foreach (get_search_arr($key ? $key : 'compare_operator') as $operator => $lang_key) {
            $operator_list[] = array('value' => $operator, 'text' => lang($lang_key));
        }
        return array(EXT_JSON_READER_ROOT => $operator_list);
    }

// --------------------------------------------------------------------

    /**
     * Is this a ajax request
     *
     * @access protected
     * @return bool
     */
    protected function is_ajax() {
        return $this->input->is_ajax_request();
    }

// --------------------------------------------------------------------

    /**
     * set output
     *
     * The sub class could override this method to extend the output type
     *
     * @access protected
     * @param array or string or xml etc...
     * @return void
     */
    protected function set_output($output) {
        $type = gettype($output);

        if ($type == 'array') {
            $this->output_json($output);
        }

        if ($type == 'string') {
            $this->output_string($output);
        }
    }

// --------------------------------------------------------------------

    /**
     * set output to a json string
     *
     * @access private
     * @param array
     * @return void
     */
    private function output_json($output) {
        $this->output->set_content_type('application/json')->set_output(json_encode($output));
    }

// --------------------------------------------------------------------

    /**
     * set output to a string
     *
     * @access private
     * @param string
     * @return void
     */
    private function output_string($output) {
        $this->output->set_content_type('text/plain')->set_output($output);
    }

    /**
     * Get username
     *
     * @access public
     * @return string
     */
    public function get_username() {
        return $this->_user_name;
    }

    /**
     * Get organization_id
     *
     * @access public
     * @return string
     */
    public function get_organization_id() {
        return $this->_organization_id;
    }

    /**
     * Get node_id
     *
     * @access public
     * @return string
     */
    public function get_node_id($organization_id = NULL) {
        $this->load->model('organization/organization_model');
        if (is_null($organization_id)) {
            $organization_id = $this->_organization_id;
        }
        $org_arr = $this->organization_model->get_data($organization_id);
        return $org_arr['fd_nodeid'];
    }

    /**
     * 取机构允许最大记录数
     * @return type
     */
    public function get_organization_max($fd_organizationid, $fd_resource) {
//取机构的允许最大最小记录字段
        if (is_null($this->_organization_max)) {
            $this->load->model('organization/organization_model');
            $org_arr = $this->organization_model->get_resource($fd_organizationid, $fd_resource);
            $this->_organization_max = $org_arr['fd_max'];
        }
        return $this->_organization_max;
    }

    /**
     * 取机构允许最小记录数
     * @return type
     */
    public function get_organization_min() {
//取机构的允许最大最小记录字段
        if (is_null($this->_organization_min)) {
            $this->load->model('organization/organization_model');
            $org_arr = $this->organization_model->get_resource($fd_organizationid, $fd_resource);
            $this->_organization_min = $org_arr['fd_min'];
        }
        return $this->_organization_min;
    }

    /**
     * Get get_allowed_organization_id
     * 取登录人的机构id 如果非0机构则对 增、删、改、查 进行筛选
     * @access public
     * @return string
     */
    public function get_allowed_organization_id($fd_organizationid = 0) {
        if ($this->_organization_id > 0) {
            return $this->_organization_id;
        } else {
            return $fd_organizationid;
        }
    }

    /**
     * Get group_id
     *
     * @access public
     * @return string
     */
    public function get_group_id() {
        return $this->group_id;
    }
}